package com.pearadmin.pro.modules.not.rest;

import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.pearadmin.pro.common.aop.annotation.Log;
import com.pearadmin.pro.common.constant.ControllerConstant;
import com.pearadmin.pro.common.context.UserContext;
import com.pearadmin.pro.common.web.base.BaseController;
import com.pearadmin.pro.common.web.domain.Result;
import com.pearadmin.pro.modules.not.domain.*;
import com.pearadmin.pro.modules.not.service.SysAnswerService;
import com.pearadmin.pro.modules.sys.domain.SysUser;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.log4j.Log4j2;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.*;


/**
 * 答疑区控制器
 *
 * @author ZhouXingYun
 * @date 2022/5/16 15:33
 */
@Log4j2
@Api(tags = {"答疑区"})
@RestController
@RequestMapping(ControllerConstant.PREFIX_SYS + "answer")
public class SysAnswerController extends BaseController {


    @Resource
    private SysAnswerService sysAnswerService;
    @Resource
    private UserContext userContext;


    /**
     * 答疑区列表
     *
     * @return java.lang.Object
     * @author ZhouXingYun
     * @date 2022/5/16 15:35
     */
    @RequestMapping("getAnswerList")
    @Log(title = "获取答疑区列表")
    @ApiOperation(value = "获取答疑区列表")
    public Object getAnswerList() {
        List<SysAnswer> answerList = sysAnswerService.getAnswerList();
        return success(answerList);
    }


    /**
     * 新增讨论主题
     *
     * @param param 讨论区实体
     * @return com.pearadmin.pro.common.web.domain.Result
     * @author ZhouXingYun
     * @date 2022/5/17 9:17
     */
    @PostMapping("save")
    @Log(title = "新增讨论主题")
    @ApiOperation(value = "新增讨论主题")
    public Result save(@RequestBody Map<String, String> param) {
//        sysAnswer.setDialogueId(IdWorker.getIdStr());
//        boolean save = sysAnswerService.save(sysAnswer);
//        return auto(save);
        SysAnswer sysAnswer = new SysAnswer();
        sysAnswer.setDialogueId(IdWorker.getIdStr());
        sysAnswer.setTitle(param.get("title"));
        sysAnswer.setDescription(param.get("describe"));
        sysAnswer.setContent(param.get("content"));
        sysAnswer.setImg(param.get("img"));
        sysAnswer.setParentId("0");
        sysAnswer.setSendTime(new Date());
        sysAnswer.setCreateBy(userContext.getUserId());
        sysAnswer.setUpdateBy(userContext.getUserId());
        sysAnswer.setUpdateTime(new Date());
        sysAnswer.setOpenTheEdit("0");
        sysAnswer.setPageView(1000);
        boolean save = sysAnswerService.save(sysAnswer);
        return auto(save);
    }


    /**
     * 删除发布内容
     *
     * @param id 讨论编号
     * @return {@link Result}
     */
    @DeleteMapping("remove")
    @Log(title = "删除讨论")
    @ApiOperation(value = "删除讨论")
    public Result remove(@RequestParam String id) {
        return auto(sysAnswerService.removeById(id));
    }


    /**
     * 查询评论内容
     *
     * @param id 查询参数 对话编号
     * @return com.pearadmin.pro.common.web.domain.Result
     * @author ZhouXingYun
     * @date 2022/5/17 15:17
     */
    @GetMapping("getLetterCount")
    @Log(title = "查询评论内容")
    @ApiOperation(value = "查询评论内容")
    public Result getLetterCount(@RequestParam String id) {
        Map<String, Object> returnMap = new HashMap<>();
        SysAnswer sysAnswer = sysAnswerService.getById(id);
       List<SysAnswerList> list = sysAnswerService.getComment(sysAnswer.getDialogueId());
        System.out.println("list："+list);
        returnMap.put("content",sysAnswer.getContent());
        returnMap.put("Comment",list);
        return success(returnMap);
    }


    /**
     * 新增评论
     * @author ZhouXingYun
     * @date 2022/5/17 18:59
     * @param sysAnswerList 评论实体类
     * @return com.pearadmin.pro.common.web.domain.Result
     */
    @PostMapping("savComment")
    @Log(title = "新增评论")
    @ApiOperation(value = "新增评论")
    public Result savComment(@RequestBody SysAnswerList sysAnswerList) {
        String userId = userContext.getUserId();
        sysAnswerList.setCreateBy(userId);
        boolean a = sysAnswerService.savComment(sysAnswerList);
        return auto(a);
    }
}
